package de.schillermann.glowstonemysql;

import java.sql.SQLException;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author Mario Schillermann
 */
final public class GlowstoneMysql extends JavaPlugin
{
    private MysqlApi database = null;
    
    @Override
    public void onEnable()
    {
        this.saveDefaultConfig();

        this.database = new MysqlApi(
            this.getConfig().getString("database.url"),
            this.getConfig().getString("database.database"),
            this.getConfig().getString("database.user"),
            this.getConfig().getString("database.password")
        );

        if(this.database.getConnection() == null)
        {
            this.getServer().getPluginManager().disablePlugin(this);
        }
        
        this.getServer().getServicesManager().register(
            MysqlApi.class,
            this.database,
            this,
            ServicePriority.Normal
        );
    }
    
    @Override
    public void onDisable()
    {
        try
        {
            this.database.getConnection().close();
        }
        catch (SQLException ex)
        {
            System.err.println(
                "Can not close MySQL Connection! Error: " + ex.getMessage()
            );
        }
    }
}
